package com.ruoyi.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.base.domain.SysPermission;
import com.ruoyi.base.mapper.SysPermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.SysApiapplyDataMapper;
import com.ruoyi.base.domain.SysApiapplyData;
import com.ruoyi.base.service.ISysApiapplyDataService;

/**
 * 接口访问申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-03
 */
@Service
public class SysApiapplyDataServiceImpl implements ISysApiapplyDataService {
    @Autowired
    private SysApiapplyDataMapper sysApiapplyDataMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysPermissionServiceImpl sysPermissionServiceImpl;
    /**
     * 查询接口访问申请
     *
     * @param id 接口访问申请主键
     * @return 接口访问申请
     */
    @Override
    public SysApiapplyData selectSysApiapplyDataById(String id) {
        return sysApiapplyDataMapper.selectSysApiapplyDataById(id);

    }

    /**
     * 查询接口访问申请列表
     *
     * @param sysApiapplyData 接口访问申请
     * @return 接口访问申请
     */
    @Override
    public List<SysApiapplyData> selectSysApiapplyDataList(SysApiapplyData sysApiapplyData) {
        return sysApiapplyDataMapper.selectSysApiapplyDataList(sysApiapplyData);
    }

    /**
     * 新增接口访问申请
     *
     * @param sysApiapplyData 接口访问申请
     * @return 结果
     */
    @Override
    public int insertSysApiapplyData(SysApiapplyData sysApiapplyData) {
        sysApiapplyData.setId(UUID.randomUUID().toString());
        int row =  sysApiapplyDataMapper.insertSysApiapplyData(sysApiapplyData);
        if(row>0){
            SysPermission sysPermission = sysPermissionMapper.selectSysPermissionById(sysApiapplyData.getId());
            sysPermission.setApplycount(sysPermission.getApplycount()+1);
            Integer score =  sysPermissionServiceImpl.getSysPermissionScore(sysPermission);
            sysPermission.setScore(score);
            sysPermissionMapper.updateSysPermission(sysPermission);
        }
        return row;

    }

    /**
     * 修改接口访问申请
     *
     * @param sysApiapplyData 接口访问申请
     * @return 结果
     */
    @Override
    public int updateSysApiapplyData(SysApiapplyData sysApiapplyData) {
        sysApiapplyData.setAppsecret(UUID.randomUUID().toString().replace("-", ""));
        sysApiapplyData.setApprovetime(new Date());
        return sysApiapplyDataMapper.updateSysApiapplyData(sysApiapplyData);
    }

    /**
     * 批量删除接口访问申请
     *
     * @param ids 需要删除的接口访问申请主键
     * @return 结果
     */
    @Override
    public int deleteSysApiapplyDataByIds(String[] ids) {
        return sysApiapplyDataMapper.deleteSysApiapplyDataByIds(ids);
    }

    /**
     * 删除接口访问申请信息
     *
     * @param id 接口访问申请主键
     * @return 结果
     */
    @Override
    public int deleteSysApiapplyDataById(String id) {
        return sysApiapplyDataMapper.deleteSysApiapplyDataById(id);
    }
}
